package com.txc.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

//druid连接池配置
@Configuration
public class DruidConfig {

  @ConfigurationProperties(prefix = "spring.datasource")
  @Bean
  public DataSource druidDataSource(){
     return new DruidDataSource();
  }


  //后台监控 web.xml
  @Bean
  public ServletRegistrationBean statViewServlet(){
    ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(),"/druid/*");
    //后台需要有人登陆进行查看 账号密码配置
    HashMap<String,String> map = new HashMap<>();
    //登陆的key是固定的
    map.put("loginUsername","admin");
    map.put("loginPassword","123456");
    //允许谁访问
    map.put("allow","");
    bean.setInitParameters(map);//初始化参数
    return bean;
  }

  //过滤器
  @Bean
  public FilterRegistrationBean webStatFileter(){
    FilterRegistrationBean webFileter = new FilterRegistrationBean();
    webFileter.setFilter(new WebStatFilter());
    //可以过滤哪些请求
    Map<String, String> initParameters = new HashMap<>();
    //这些东西不进行统计
    initParameters.put("exclusions","*.js,*.css,/druid/*");
    webFileter.setInitParameters(initParameters);
    return webFileter;
  }
}
